package com.lanou3g.ptp.sso.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.entity.Teacher;
import com.lanou3g.ptp.entity.User;
import com.lanou3g.ptp.sso.mapper.SsoMapper;
import com.lanou3g.ptp.sso.service.SsoService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.teacher.service.TeacherService;
import com.lanou3g.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class SsoServiceImpl implements SsoService {

    @Autowired
    private SsoMapper ssoMapper;

    @Reference
    private StudentService studentService;

    @Reference
    private TeacherService teacherService;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${REDIS_TOKEN_KEY_PREFIX}")
    private String REDIS_TOKEN_KEY_PREFIX;

    @Value("${REDIS_TOKEN_KEY_EXPIRE}")
    private Long REDIS_TOKEN_KEY_EXPIRE;

    @Override
    public String ssoUser(String name, String password) {
        System.out.println("进入方法");
        String ssoUserKey = null;
        //通过 name  password 找到对应用户
        String t =null;
        try {
             t = MD5Utils.md5(password, "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
        User user = ssoMapper.userByName(name, t);
        if (user != null) {
            //通过user id 查询 个人详细信息
            Student student = studentService.studentById(user.getId());
            if (student != null) {
                student.setUser(user);
                String s = JSON.toJSONString(student);//对象序列化
                System.out.println("序列化" + s);
                //获取uuid 去掉空格
                String s1 = UUID.randomUUID().toString().replaceAll("\\-", "");
                //设置key值
                ssoUserKey = REDIS_TOKEN_KEY_PREFIX + s1;
                //用户信息存入redis
                redisTemplate.opsForValue().set(ssoUserKey, s, REDIS_TOKEN_KEY_EXPIRE, TimeUnit.MINUTES);
            } else {
                // 没有在学生表中找到学生
                //去查询老师表
                Teacher teacher = teacherService.findById(user.getId());
                teacher.setUser(user);
                //判定是不是学校老师  0为本校，1为校外
                String s = JSON.toJSONString(teacher);//对象序列化
                System.out.println("序列化" + s);
                //获取uuid 去掉空格
                ssoUserKey = UUID.randomUUID().toString().replaceAll("\\-", "");
                //设置key值
                String s1 = REDIS_TOKEN_KEY_PREFIX + ssoUserKey;
                //用户信息存入redis
                redisTemplate.opsForValue().set(s1, s, REDIS_TOKEN_KEY_EXPIRE, TimeUnit.MINUTES);
            }
        }else {
            ssoUserKey = "on";
        }
        //返回token
        return ssoUserKey;
    }

    //通过token查用户
    @Override
    public Object tokenUser(String token) {
        //通过key值 获取value
        String s1 = REDIS_TOKEN_KEY_PREFIX + token;
        String s = redisTemplate.opsForValue().get(s1);
        //序列化成map格式  查找key值中有没有 对应的值
        boolean b = JSON.parseObject(s).containsKey("type");
        if (b) {
            redisTemplate.expire(token,REDIS_TOKEN_KEY_EXPIRE, TimeUnit.MINUTES);
            //有type 是老师
            //映射成老师
            return JSON.parseObject(s, Teacher.class);
        } else {
            redisTemplate.expire(token,REDIS_TOKEN_KEY_EXPIRE, TimeUnit.MINUTES);
            //没有type 是学生
            //映射成学生
            return JSON.parseObject(s, Student.class);
        }
    }
}
